package de.koller.worlders.gameplay.gui;

import de.koller.oceanFramework.gl.OceanGame;
import de.koller.oceanFramework.glyph.GlyphRendererGL;
import de.koller.oceanFramework.glyph.GlyphScreenGL;
import de.koller.oceanFramework.tree.Child;
import de.koller.worlders.WSea;
import de.koller.worlders.gameplay.resource.FontValue;
import de.koller.worlders.render.IGLRender;
import java.util.Arrays;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;

public class DebugData extends WSea implements IGLRender {
	
	@Child private final GlyphScreenGL gscreen = new GlyphScreenGL( 512, 512,
			new FontValue(), "de/koller/worlders/gameplay/resource/font.png" );
	
	private OceanGame ogame;
	private boolean visible = false;
	private boolean slowed = false;
	
	private int tindex = 0;
	private byte[] tTime = new byte[ 63 ];
	private boolean[] tLCacl = new boolean[ 63 ];
	
	private int ltclear = 0;
	
	@Override
	protected void init() {
		super.init();
		
		ogame = (OceanGame)wroot.getGame();
		wroot.rendering.addGUIElement( this );
	}

	@Override
	protected void release() {
		super.release();
		
		wroot.rendering.removeGUIElement( this );
	}
	
	@Override
	public void renderGL( int elapsedTime ) {
		if( !visible )
			return;
		
		GlyphRendererGL r = gscreen.getRenderer();
		r.setCurrentFontColor( 0xffffffff );
		r.setCurrentBackColor( 0xff000000 );
		
		r.renderString( 0, 0, "Time: " + wroot.getTime() );
		r.renderString( 0, 2, "UnTime: " + Math.round( ogame.getUnprocessedSeconds() * 1000.)
				+ " F: " + ogame.getFrameTimeAverage() + " ms/frame"
				+ " U: " + ogame.getUpdateTimeAverage() + " ms/frame" 
				+ " M: " + ogame.getUsedSpace() + " / " + ogame.getMaxSpace() );
		r.renderString( 0, 3, "Camera: " + (int)wroot.getTileMap().getOffsetX() +
				" / " + (int)wroot.getTileMap().getOffsetY() 
				+ " Player: " + wroot.gamePlayer.getGPO().getAABB().getPosition().getXi()
				+ " / " + wroot.gamePlayer.getGPO().getAABB().getPosition().getYi() );
		
		tindex++;
		tindex %= 63;
		
		r.renderString( 0, 5, "T" );
		r.renderString( 0, 6, "L" );
		for( int i = 0; i < 63; i++ ) {
			drawIndex( i );
		}
		
		ltclear = wroot.getTileMap().tileHolder.getRemovedTiles();
		
		r.setCurrentFontColor( 0xffffffff );
		r.setCurrentBackColor( 0xff000000 );
		r.renderString( 0, 8, "Buffer: " + wroot.getBuffers().getBuffer().size() );
		r.renderString( 0, 9, "GUIE: " + wroot.rendering.getGUIElementNumber()
				+ " WorldE: " + wroot.rendering.getWorldElementNumber()
				+ " TWorldE: " + wroot.rendering.getTransparentWorldElementNumber() );
		r.renderString( 0, 10, "Lights: " + wroot.getTileMap().getLight().getLightSourcesNumber() 
				+ " Tiles: " + wroot.getTileMap().tileHolder.getTileNumber()
				+ " Maps: " + wroot.getTileMap().tileHolder.getLoadedMaps()
				+ " Tclear: " + ltclear
				+ " TA: " + wroot.getTileMap().tileHolder.getAccessPerFrame() );
		r.renderString( 0, 11, Arrays.toString( wroot.getTileMap().tileHolder.getMapDataTiles() ) );
		
		wroot.getTileMap().tileHolder.resetAccesPerFrame();
		GL11.glColor3f( 1, 1, 1 );
		
		this.gscreen.updateBuffer();
		this.gscreen.getTexture().draw2D( 10, 50, 512, 512, 50 );
	}

	private void drawIndex( int i ) {
		GlyphRendererGL r = gscreen.getRenderer();
		
		if( tindex == i ) {
			r.setCurrentBackColor( 0xff0000ff );
			r.renderGlyph( i+1, 5, -1, -1 );
			double t = ogame.getLastTimeDebug();
			t /= 16_000_000d;
			byte dt = (byte)Math.round( t * 10d );
			if( dt > 10 ) dt = 9; 
			tTime[i] = dt;
			
			tLCacl[i] = wroot.getTileMap().getLight().debugLightCalcTime == 0;
			return;
		}
		
		r.setCurrentBackColor( 0xff000000 );
		
		byte td = tTime[i];
		if( td < 5 )
			r.setCurrentFontColor( 0xff00ff00 );
		else if( td < 8 )
			r.setCurrentFontColor( 0xffffff00 );
		else 
			r.setCurrentFontColor( 0xffff0000 );
		
		r.renderString( i+1, 5, td + "" );
		
		if( tLCacl[i] ) {
			r.setCurrentBackColor( 0xff00ff00 );
			r.renderGlyph( i+1, 6, -1, -1 );
		}
			
	}
	
	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		if( wroot.getInput().isKeyPressed( Keyboard.KEY_F4 ) )
			visible = !visible;
		if( wroot.getInput().isKeyPressed( Keyboard.KEY_F6 ) )
			slowed = !slowed;
		
		if( slowed )
			try { Thread.sleep( 100 ); } catch( InterruptedException ex ) {}
	}
	
}
